package com.dw.love.dwade.common;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by luozizhao on 2018/6/19.
 */
//https://blog.csdn.net/chenhuakang/article/details/52756964 安卓存储权限及存储文件
public class FileManager {

    public static final String TAG = "FileManager";
    public String mRootFile;
    public Context mContext;
    public static final String IMAGEPATH = File.separator + "Wade/Image/";// 图片存储路径
    private static FileManager mFileManagerInstance;


    private FileManager(Context context) {
        this.mContext = context;
    }

    public static FileManager getInstance(Context context) {
        if (mFileManagerInstance == null) {
            synchronized (FileManager.class) {
                if (mFileManagerInstance == null) {
                    mFileManagerInstance = new FileManager(context);
                }
            }
        }
        return mFileManagerInstance;
    }


    public String getBitmapEtrFilePath() {
        if (TextUtils.isEmpty(mRootFile)) {
//            mRootFile = mContext.getExternalFilesDir(null).getPath(); // 应用内不用申请权限

            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                mRootFile = Environment.getExternalStorageDirectory().getAbsolutePath();
                Log.d(TAG, "getBitmapEtrFilePath: sd卡");
            }else {
                Log.d(TAG, "getBitmapEtrFilePath: 内部存储");
                mRootFile = mContext.getFilesDir().getAbsolutePath();// /data/目录下
            }
        }
        return mRootFile + IMAGEPATH;
    }


    public String addBitmapToCache(String url, Bitmap bitmap) {// 保存成功返回 文件目录；失败返回null
        if (TextUtils.isEmpty(url)) {
            return null;
        }

        if (url.startsWith("http://")||url.startsWith("https://")) {
            int indexOf = url.lastIndexOf("/");
            url = url.substring(indexOf,url.length());
        }

        //01 文件夹
        File file = new File(getBitmapEtrFilePath());
        if (!file.exists()) {
            file.mkdirs();
            //TODO
        }
        //02 文件
        File file1 = new File(getBitmapEtrFilePath(), url);// 试试不加‘\’看看方法会不会自动添加
        //03 写入
        try {
            OutputStream os = new FileOutputStream(file1);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
            os.flush();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.d(TAG, "addBitmapToCache: "+e.getMessage());
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "addBitmapToCache: "+e.getMessage());
            return null;
        }
        return file1.getAbsolutePath();
    }


    public Bitmap getBitmapFormCache(String fileName) {
        Bitmap bitmap = null;
        File file = new File(getBitmapEtrFilePath(), fileName);
        if (!file.exists()) {
            return bitmap;
        }
        try {
            FileInputStream fis = new FileInputStream(file);
            bitmap = BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
            Log.d(TAG, "getBitmapFormCache: "+e.getMessage());
            e.printStackTrace();
        }
        return bitmap;
    }

}
